import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Test } from '@src/common/entities/test.entity';
import { RedisClient } from '@src/common/providers/redisClient.service';

@Injectable()
export class TestDao {
  constructor(
    @InjectRepository(Test)
    private readonly TestRepository: Repository<Test>,
    private readonly redis: RedisClient,
  ) {

  }

  async getOneTestFromDB(id: number) {
    let test = await this.TestRepository.findOne({
      where: { id: id },
      cache: false
    })

    return test
  }

  async getOneTestFromRedis(id: number) {
    let test = await this.redis.client.get('id_' + id)
    return test
  }

  async saveOneTest(transac, data: any) {
    return await transac.save(Test, data)
  }

  async deleteOneTestFromDB(transac, data) {
    return await transac.remove(data)
  }

  async deleteOneTestFromRedis(id: number) {
    await this.redis.client.del('id_' + id)
  }
}